// pages/answer/answer.js

//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    typelist: [{
      typeId: 1,
      typeName: "全部",
    }, {
      typeId: 2,
      typeName: "精华",
    }, {
      typeId: 3,
      typeName: "热门",
    }, {
      typeId: 4,
      typeName: "最新",
    }],
    typeSelected: 1,
    answerTitle:'',
    answerContent:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  chooseType:function(res){
    var that = this;
    that.setData({
      typeSelected: res.target.dataset.selected
    });
  },
  getTitle:function(e){
    this.setData({
      answerTitle: e.detail.value
    })
  },
  getContent:function(e){
    this.setData({
      answerContent: e.detail.value
    })
  },
  submitAnswer:function(res){
    var that = this;
    var dataform = {
      "answerTitle": that.data.answerTitle,
      "answerContent": that.data.answerContent,
      "answerType": that.data.typeSelected
    };
    wx.request({
      url: 'http://localhost:8080/wxchat/superadmin/addAnswer',
      method:'POST',
      data:JSON.stringify(dataform),
      header:{
        'Content-Type': 'application/json'
      },
      success:function(res){
        var result = res.data.success;
        var toastText = '创建成功';
        if (result) {
          that.setData({
            title: '',
            content:'',
            typeSelected:1
          });
        } else {
          toastText = '操作失败' + res.data.errMsg;
        }
        wx.showToast({
          title: toastText,
          icon: '',
          duration: 2000
        });
        wx.switchTab({
          url: '../answerlist/answerlist',
        })
      }
    })
  }
})